package com.springBoot.springBootSysCore.modules.controller.api.system;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.springBoot.springBootSysCore.common.StaticConfig;
import com.springBoot.springBootSysCore.common.utils.returnUtil.PageUtils;
import com.springBoot.springBootSysCore.modules.controller.web.base.BaseController;
import com.springBoot.springBootSysCore.modules.entity.system.SysMessage;
import com.springBoot.springBootSysCore.modules.entity.utils.UserUtils;
import com.springBoot.springBootSysCore.modules.enums.base.DeleteStateEnum;
import com.springBoot.springBootSysCore.modules.services.system.SysMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 *消息
 */
@RestController
@RequestMapping("${apiPath}/auth/sysMessage")
public class SysMessageControllerApi extends BaseController<SysMessage,String> {
    @Autowired
    private SysMessageService sysMessageService;

    /**
     * 查询消息
     * @param request
     * @param model
     * @param type
     * @param pageNumber
     * @param pageSize
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping(value="getMessage",method= RequestMethod.POST)
    public Object getMessage(HttpServletRequest request,Model model,
                                @RequestParam(required = true)String type,
                                @RequestParam(required = false ,defaultValue = "1")int pageNumber,
                                @RequestParam(required = false ,defaultValue = "10")int pageSize) throws JsonProcessingException{
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("EQ_delFlag", DeleteStateEnum.NO_DELETE.getVal());
        map.put("EQ_messAgeType", type);
        map.put("EQ_toSysUser.id", UserUtils.getByAccount(request.getParameter(StaticConfig.PARAM_USERNAME)).getId());
        if(type.equals("2")){
//            map.put("EQ_@sysMessageLog#sysMessageLogs#sysUser#id",UserUtils.getByAccount(request.getParameter(StaticConfig.PARAM_USERNAME)).getId());
//            map.put("ISFALSE_@sysMessageLog#sysMessageLogs#readed", null);
            return sysMessageService.getPublicMessage(request,PageUtils.buildPageRequest(pageNumber,pageSize,"create_date","desc"));
        }/*else{
            map.put("ISFALSE_readed", null);
        }*/
        return super.queryByPageForJson(pageNumber,pageSize,model, request,map, "createDate", "desc");
    }

    /**
     * 统计消息
     * @param request
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping(value="countMessage",method= RequestMethod.POST)
    public Object countMessage(HttpServletRequest request) throws JsonProcessingException{
        return sysMessageService.countMessage(request);
    }
    /**
     * 全部已读
     * @param request
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping(value="readAllMessage",method= RequestMethod.POST)
    public Object readAllMessage(HttpServletRequest request,@RequestParam(required = true)String type) throws JsonProcessingException{
        return sysMessageService.readAllMessage(request,type);
    }
    /**
     * 已读消息
     * @param request
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping(value="readMessage",method= RequestMethod.POST)
    public Object readMessage(HttpServletRequest request,@RequestParam(required = true)String id) throws JsonProcessingException{
        return sysMessageService.readMessage(request,id);
    }
    /**
     * 删除消息
     * @param request
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping(value="deleteMessage",method= RequestMethod.POST)
    public Object deleteMessage(HttpServletRequest request,@RequestParam(required = true)String id) throws JsonProcessingException{
        return sysMessageService.deleteMessage(request,id);
    }


}
